package service

import (
	"GopherFocus/app/dao"
	"GopherFocus/app/system/index/internal/define"
	"context"
)

var Search = searchService{}

type searchService struct{}

// 搜索内容列表
func (s *searchService) Search(ctx context.Context, input define.ContentSearchInput) (output *define.ContentSearchOutput, err error) {
	var (
		m           = dao.Content.Ctx(ctx)
		likePattern = `%` + input.Key + `%`
	)
	output = &define.ContentSearchOutput{
		Page: input.Page,
		Size: input.Size,
	}
	m = m.WhereLike(dao.Content.C.Content, likePattern)
}

/*
// 搜索内容列表
func (s *contentService) Search(ctx context.Context, input define.ContentSearchInput) (output *define.ContentSearchOutput, err error) {
	var (
		m           = dao.Content.Ctx(ctx)
		likePattern = `%` + input.Key + `%`
	)
	output = &define.ContentSearchOutput{
		Page: input.Page,
		Size: input.Size,
	}
	m = m.WhereLike(dao.Content.C.Content, likePattern).WhereOrLike(dao.Content.C.Title, likePattern)
	// 内容类型
	if input.Type != "" {
		m = m.Where(dao.Content.C.Type, input.Type)
	}
	// 栏目检索
	if input.CategoryId > 0 {
		idArray, err := Category.GetSubIdList(ctx, input.CategoryId)
		if err != nil {
			return nil, err
		}
		m = m.Where(dao.Content.C.CategoryId, idArray)
	}
	listModel := m.Page(input.Page, input.Size)
	switch input.Sort {
	case model.ContentSortHot:
		listModel = listModel.OrderDesc(dao.Content.C.ViewCount)
	case model.ContentSortActive:
		listModel = listModel.OrderDesc(dao.Content.C.UpdatedAt)
	//case model.ContentSortScore:
	//	listModel = listModel.OrderDesc("score")
	default:
		listModel = listModel.OrderDesc(dao.Content.C.Id)
	}
	all, err := listModel.All()
	if err != nil {
		return nil, err
	}
	// 没有数据
	if all.IsEmpty() {
		return output, nil
	}
	output.Total, err = m.Count()
	if err != nil {
		return nil, err
	}
	// 搜索统计
	statsModel := m.Fields(dao.Content.C.Type, "count(*) total").Group(dao.Content.C.Type)
	statsAll, err := statsModel.All()
	if err != nil {
		return nil, err
	}
	output.Stats = make(map[string]int)
	for _, v := range statsAll {
		output.Stats[v["type"].String()] = v["total"].Int()
	}
	// Content
	if err := all.ScanList(&output.List, "Content"); err != nil {
		return nil, err
	}
	// Category
	err = dao.Category.
		Fields(model.ContentListCategoryItem{}).
		Where(dao.Category.C.Id, gutil.ListItemValuesUnique(output.List, "Content", "CategoryId")).
		ScanList(&output.List, "Category", "Content", "id:CategoryId")
	if err != nil {
		return nil, err
	}
	// User
	err = dao.User.
		Fields(model.ContentListUserItem{}).
		Where(dao.User.C.Id, gutil.ListItemValuesUnique(output.List, "Content", "UserId")).
		ScanList(&output.List, "User", "Content", "id:UserId")
	if err != nil {
		return nil, err
	}

	return output, nil
}
*/
